﻿#region 头注释
/******************************************************************************
文件名称    :   SurgeryInfection.cs
创建日期    :   2011/5/11 22:22:55
功能描述    :   SurgeryInfection实体类
修改日志    :   
    2011-12-13 22:22  马利
*******************************************************************************/
#endregion

using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using Newtonsoft.Json;
using Dare.Utilities.Data;

namespace Stone.Components.Entities
{
    /// <summary>
    /// SurgeryInfection实体类
    /// </summary>
    public class SurgeryInfection : EntityBase<SurgeryInfection, long>
    {
        #region 构造函数

        /// <summary>
        /// 默认构造函数
        /// </summary>
        public SurgeryInfection()
            : base()
        {
            base.xmlSerializer = new DefaultXmlSerializer(this);
            base.jsonSerializer = new DefaultJsonSerializer(this);
        }

        public override void Reset()
        {
            fever = null;
            disease = null;
            antibioticsType = null;
            antibioticsTime = null;
            diseaseCure = null;
            blood = null;
            comment = null;
            timeStamp = DateTime.MinValue;
        }

        public override SurgeryInfection CreateNew()
        {
            return new SurgeryInfection();
        }

        #endregion

        #region 属性

        /// <summary>
        /// 获取实体类型
        /// </summary>
        public override EntityType EntityType
        {
            get { return EntityType.SurgeryInfection; }
        }

        /// <summary>
        /// 获取或设置是否发热
        /// </summary>
        public bool? Fever
        {
            get { return fever; }
            set
            {
                if (entityState == EntityState.Saved && fever != value) entityState = EntityState.Changed;
                fever = value;
            }
        }
        protected bool? fever;

        /// <summary>
        /// 获取或设置是否发生脓毒血症
        /// </summary>
        public bool? Disease
        {
            get { return disease; }
            set
            {
                if (entityState == EntityState.Saved && disease != value) entityState = EntityState.Changed;
                disease = value;
            }
        }
        protected bool? disease;

        /// <summary>
        /// 获取或设置使用抗生素种类
        /// </summary>
        public int? AntibioticsType
        {
            get { return antibioticsType; }
            set
            {
                if (entityState == EntityState.Saved && antibioticsType != value) entityState = EntityState.Changed;
                antibioticsType = value;
            }
        }
        protected int? antibioticsType;

        /// <summary>
        /// 获取或设置使用抗生素时间
        /// </summary>
        public int? AntibioticsTime
        {
            get { return antibioticsTime; }
            set
            {
                if (entityState == EntityState.Saved && antibioticsTime != value) entityState = EntityState.Changed;
                antibioticsTime = value;
            }
        }
        protected int? antibioticsTime;

        /// <summary>
        /// 获取或设置是否治愈
        /// </summary>
        public bool? DiseaseCure
        {
            get { return diseaseCure; }
            set
            {
                if (entityState == EntityState.Saved && diseaseCure != value) entityState = EntityState.Changed;
                diseaseCure = value;
            }
        }
        protected bool? diseaseCure;

        /// <summary>
        /// 获取或设置是否出血
        /// </summary>
        public bool? Blood
        {
            get { return blood; }
            set
            {
                if (entityState == EntityState.Saved && blood != value) entityState = EntityState.Changed;
                blood = value;
            }
        }
        protected bool? blood;

        /// <summary>
        /// 获取或设置备注
        /// </summary>
        public string Comment
        {
            get { return comment; }
            set
            {
                if (entityState == EntityState.Saved && comment != value) entityState = EntityState.Changed;
                comment = value;
            }
        }
        protected string comment;

        /// <summary>
        /// 获取或设置更新时间戳
        /// </summary>
        public DateTime TimeStamp
        {
            get { return timeStamp; }
            set
            {
                if (entityState == EntityState.Saved && timeStamp != value) entityState = EntityState.Changed;
                timeStamp = value;
            }
        }
        protected DateTime timeStamp;

        #endregion

        #region 方法

        #endregion

        #region 实体复制相关方法

        /// <summary>
        /// 复制当前实体属性到指定实体上
        /// </summary>
        /// <param name="entity">要把属性复制到的实体</param>
        /// <returns>返回传入的实体</returns>
        public override SurgeryInfection CopyTo(SurgeryInfection entity)
        {
            if (base.CopyTo(entity) == null) return null;

            entity.fever = this.fever;
            entity.disease = this.disease;
            entity.antibioticsType = this.antibioticsType;
            entity.antibioticsTime = this.antibioticsTime;
            entity.diseaseCure = this.diseaseCure;
            entity.blood = this.blood;
            entity.comment = this.comment;
            entity.timeStamp = this.timeStamp;

            return entity;
        }

        #endregion

        #region 默认XML序列器类

        public class DefaultXmlSerializer : XmlSerializerBase, IEntityXmlSerializer<SurgeryInfection>
        {
            SurgeryInfection entity;
            public DefaultXmlSerializer(SurgeryInfection entity)
                : base(entity.GetType().Name)
            {
                this.entity = entity;
            }

            public SurgeryInfection Entity
            {
                get { return entity; }
                set { entity = value; }
            }

            protected override void BeforeReadXml()
            {
                entity.Reset();
            }

            /// <summary>
            /// 读取XML属性
            /// </summary>
            /// <param name="name">属性名</param>
            /// <param name="value">属性值</param>
            /// <param name="reader">XML读取器</param>
            internal override void ReadXmlAttribute(string name, string value, XmlReader reader)
            {
                switch (name)
                {
                    case "id":
                        entity.id = XmlConvert.ToInt64(value);
                        break;
                    case "timeStamp":
                        entity.timeStamp = new DateTime(long.Parse(value));
                        break;
                    case "entityState":
                        entity.entityState = (EntityState)Enum.Parse(typeof(EntityState), value, true);
                        break;
                }
            }

            /// <summary>
            /// 读取XML子节点
            /// </summary>
            /// <param name="name">节点名</param>
            /// <param name="reader">XML读取器</param>
            internal override void ReadXmlChildElement(string name, XmlReader reader)
            {
                switch (name)
                {
                    case "Fever":
                        entity.fever = XmlConvert.ToBoolean(reader.ReadElementString());
                        break;
                    case "Disease":
                        entity.disease = XmlConvert.ToBoolean(reader.ReadElementString());
                        break;
                    case "AntibioticsType":
                        entity.antibioticsType = XmlConvert.ToInt32(reader.ReadElementString());
                        break;
                    case "AntibioticsTime":
                        entity.antibioticsTime = XmlConvert.ToInt32(reader.ReadElementString());
                        break;
                    case "DiseaseCure":
                        entity.diseaseCure = XmlConvert.ToBoolean(reader.ReadElementString());
                        break;
                    case "Blood":
                        entity.blood = XmlConvert.ToBoolean(reader.ReadElementString());
                        break;
                    case "Comment":
                        entity.comment = reader.ReadElementString();
                        break;
                }
            }

            /// <summary>
            /// 写入XML属性
            /// </summary>
            /// <param name="writer">XML编写器</param>
            internal override void WriteXmlAttributes(XmlWriter writer)
            {
                writer.WriteAttributeString("id", XmlConvert.ToString(entity.Id));
                writer.WriteAttributeString("timeStamp", entity.TimeStamp.Ticks.ToString());
                writer.WriteAttributeString("entityState", entity.EntityState.ToString());
            }

            /// <summary>
            /// 写入XML子节点
            /// </summary>
            /// <param name="writer">XML编写器</param>
            internal override void WriteXmlChildElements(XmlWriter writer)
            {
                if (entity.Fever != null) writer.WriteElementString("Fever", XmlConvert.ToString(entity.Fever.Value));
                if (entity.Disease != null) writer.WriteElementString("Disease", XmlConvert.ToString(entity.Disease.Value));
                if (entity.AntibioticsType != null) writer.WriteElementString("AntibioticsType", entity.AntibioticsType.Value.ToString());
                if (entity.AntibioticsTime != null) writer.WriteElementString("AntibioticsTime", XmlConvert.ToString(entity.AntibioticsTime.Value));
                if (entity.DiseaseCure != null) writer.WriteElementString("DiseaseCure", XmlConvert.ToString(entity.DiseaseCure.Value));
                if (entity.Blood != null) writer.WriteElementString("Blood", XmlConvert.ToString(entity.Blood.Value));
                if (entity.Comment != null) writer.WriteElementString("Comment", entity.Comment);
            }
        }

        #endregion

        #region 默认Json序列化器类

        public class DefaultJsonSerializer : JsonSerializerBase, IEntityJsonSerializer<SurgeryInfection>
        {
            SurgeryInfection entity;
            public DefaultJsonSerializer(SurgeryInfection entity)
                : base()
            {
                this.entity = entity;
            }

            public SurgeryInfection Entity
            {
                get { return entity; }
                set { entity = value; }
            }

            /// <summary>
            /// 读取JSON属性
            /// </summary>
            /// <param name="propName">属性名称</param>
            /// <param name="propValue">属性值</param>
            /// <param name="reader">JSON读取器</param>
            protected override void ReadJsonProperty(string propName, object propValue, JsonReader reader)
            {
                switch (propName)
                {
                    case "id":
                        entity.id = DbConvert.ToNullableInt64(propValue) ?? entity.id;
                        break;
                    case "fever":
                        entity.fever = DbConvert.ToNullableBoolean(propValue) ?? entity.fever;
                        break;
                    case "disease":
                        entity.disease = DbConvert.ToNullableBoolean(propValue) ?? entity.disease;
                        break;
                    case "antibioticsType":
                        entity.antibioticsType = DbConvert.ToNullableInt32(propValue) ?? entity.antibioticsType;
                        break;
                    case "antibioticsTime":
                        entity.antibioticsTime = DbConvert.ToNullableInt32(propValue) ?? entity.antibioticsTime;
                        break;
                    case "diseaseCure":
                        entity.diseaseCure = DbConvert.ToNullableBoolean(propValue) ?? entity.diseaseCure;
                        break;
                    case "blood":
                        entity.blood = DbConvert.ToNullableBoolean(propValue) ?? entity.blood;
                        break;
                    case "comment":
                        entity.comment = Convert.ToString(propValue) ?? entity.comment;
                        break;
                    case "timeStamp":
                        entity.timeStamp = new DateTime(DbConvert.ToNullableInt64(propValue) ?? 0);
                        break;
                }
            }

            /// <summary>
            /// 写入JSON属性
            /// </summary>
            /// <param name="writer">JSON编写器</param>
            protected override void WriteJsonProperties(JsonWriter writer)
            {

                //Id
                writer.WritePropertyName("id");
                writer.WriteValue(entity.Id);

                //是否发热
                writer.WritePropertyName("fever");
                if (entity.Fever == null)
                {
                    writer.WriteNull();
                }
                else
                {
                    writer.WriteValue(entity.Fever.Value);
                }

                //是否发生脓毒血症
                writer.WritePropertyName("disease");
                if (entity.Disease == null)
                {
                    writer.WriteNull();
                }
                else
                {
                    writer.WriteValue(entity.Disease.Value);
                }

                //使用抗生素种类
                writer.WritePropertyName("antibioticsType");
                if (entity.AntibioticsType == null)
                {
                    writer.WriteNull();
                }
                else
                {
                    writer.WriteValue((int)entity.AntibioticsType.Value);
                }

                //使用抗生素时间
                writer.WritePropertyName("antibioticsTime");
                if (entity.AntibioticsTime == null)
                {
                    writer.WriteNull();
                }
                else
                {
                    writer.WriteValue(entity.AntibioticsTime.Value);
                }

                //是否治愈
                writer.WritePropertyName("diseaseCure");
                if (entity.DiseaseCure == null)
                {
                    writer.WriteNull();
                }
                else
                {
                    writer.WriteValue(entity.DiseaseCure.Value);
                }

                //是否出血
                writer.WritePropertyName("blood");
                if (entity.Blood == null)
                {
                    writer.WriteNull();
                }
                else
                {
                    writer.WriteValue(entity.Blood.Value);
                }

                //备注
                writer.WritePropertyName("comment");
                if (entity.Comment == null)
                {
                    writer.WriteNull();
                }
                else
                {
                    writer.WriteValue(entity.Comment);
                }

                //更新时间戳
                writer.WritePropertyName("timeStamp");
                writer.WriteValue(entity.TimeStamp.Ticks);
            }
        }


        #endregion
    }

}